package com.qiyun.duijiang.service.imp;

import com.alibaba.fastjson.JSON;
import com.qiyun.duijiang.dao.BorderMapper;
import com.qiyun.duijiang.dao.JclqResultMapper;
import com.qiyun.duijiang.dao.RecordMapper;
import com.qiyun.duijiang.dao.UserMapper;
import com.qiyun.duijiang.entity.*;
import com.qiyun.duijiang.service.BprizeService;
import com.qiyun.duijiang.utils.Futil;
import com.qiyun.duijiang.utils.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BprizeServiceImp implements BprizeService {
    @Resource
    private BorderMapper borderMapper;
    @Resource
    private JclqResultMapper jclqResultMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RecordMapper recordMapper;

    @Override
    public void draw() {
        List<Border> borders = borderMapper.selectT(TimeUtil.time2());
        for (Border border:borders){
            Double authup = getPoints(border);
            win(authup,border);
        }
    }
    /**
     * 用户加点，修改订单状态
     * @param authup
     * @param border
     */
    @Transactional
    protected void win(Double authup, Border border){
        if(StringUtils.isEmpty(authup)){
            border.setStatus(3);
        }else{
            User user = new User();
            user.setUsername(border.getUsername());
            user.setAuth(authup);
            if(authup>0) {
                Record record = new Record();
                record.setCdate(TimeUtil.dayX());
                record.setWay(4);
                record.setAuth(authup);
                record.setUsername(border.getUsername());
                recordMapper.insert(record);
            }
            userMapper.updateA(user);
            border.setStatus(1);
            border.setWinauth(authup);
        }
        borderMapper.updateS(border);
    }
    /**
     * 计算方案中奖点数
     * @param border
     * @return
     */
    public Double getPoints(Border border){
        List<BorderVo1> borderVo1s = JSON.parseArray(border.getResult(),BorderVo1.class);
        if (null==borderVo1s||borderVo1s.isEmpty()){
            return 0.00;
        }
        Map<Integer,Double> map = new HashMap<>();
        for (BorderVo1 borderVo1 : borderVo1s) {
            Integer matchid = borderVo1.getMatchid();
            JclqResult jclqResult = jclqResultMapper.selectById(matchid);
            if(StringUtils.isEmpty(jclqResult)){
                return null;
            }
            String sf[] = jclqResult.getSf().split(",");
            String rf[] = jclqResult.getRf().split(",");
            String sfc[] = jclqResult.getSfc().split(",");
            String dxf[] = jclqResult.getDxf().split(",");

            String pass = borderVo1.getType();
            Double odds;
            if(pass.equals("sf")){
                odds = odds(borderVo1,sf[0]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("rf")){
                odds = odds(borderVo1,rf[1]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("sfc")){
                odds = odds(borderVo1,sfc[0]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }else if(pass.equals("dxf")){
                odds = odds(borderVo1,dxf[1]);
                if(!StringUtils.isEmpty(odds)){
                    map.put(matchid,odds);
                }
            }
        }if(map.size()<=0){
            return 0.00;
        }
        Double authup = Futil.authUp(map,border.getStrand());
        return authup*border.getNum();
    }
    public Double odds(BorderVo1 borderVo1, String s){
        boolean r = borderVo1.getChooses().contains(s);
        if (r){
            String[] arr = borderVo1.getChooses().split(",");
            String[] brr = borderVo1.getOddses().split(",");
            for (int i=0;i<arr.length;i++){
                if(s.equals(arr[i])){
                    return Double.valueOf(brr[i]);
                }
            }
        }
        return null;
    }
}
